import { BasicColumn, FormSchema } from '@/components/Table';
import { h } from 'vue';
import { Tag } from 'ant-design-vue';
import { Tinymce } from '@/components/Tinymce';

export const columns: BasicColumn[] = [
  {
    title: '页面名称',
    dataIndex: 'deptName',
    align: 'left',
  },
  {
    title: '页面Tag',
    dataIndex: 'tag',
    width: 160,
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 120,
    sorter: true,
    customRender: ({ record }) => {
      const status = record.status;
      const enable = ~~status === 0;
      const color = enable ? 'green' : 'red';
      const text = enable ? '启用' : '停用';
      return h(Tag, { color: color }, () => text);
    },
  },
  {
    title: '创建时间',
    dataIndex: 'id',
    width: 180,
    sorter: true,
    fixed: 'right',
    customRender: ({record})=>{
      return record.createTime;
    }
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '页面名称',
    component: 'Input',
    colProps: { xs: 24, xl: 12, lg: 8, xxl: 6 },
  },
  {
    field: 'tag',
    label: '页面Tag',
    component: 'Input',
    colProps: { xs: 24, xl: 12, lg: 8, xxl: 6 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'deptName',
    label: '页面名称',
    component: 'Input',
    required: true,
  },
  {
    field: 'tag',
    label: '页面Tag',
    component: 'Input',
    required: true,
  },
  {
    field: 'parentDept',
    label: '上级页面',
    component: 'TreeSelect',
    ifShow({ values }) {
      const { deptName, parentDept } = values;
      // Hide without a parentDept when editing
      return parentDept || (!deptName && !parentDept);
    },
    componentProps: {
      fieldNames: {
        label: 'deptName',
        value: 'id',
      },
      getPopupContainer: () => document.body,
    },
    required: true,
  },
  {
    field: 'status',
    label: '状态',
    component: 'RadioButtonGroup',
    defaultValue: '0',
    componentProps: {
      options: [
        { label: '启用', value: '0' },
        { label: '停用', value: '1' },
      ],
    },
    required: true,
  },
  {
    label: '页面内容',
    field: 'description',
    component: 'InputTextArea',
    rules: [{ required: true }],
    render: ({ model, field }) => {
      return h(Tinymce, {
        value: model[field],
        onChange: (value: string) => {
          model[field] = value;
        },
      });
    },
  },
];
